﻿using Common;
using DataLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TicketSystem
{
    public partial class Form_OperatorUpdate : Form
    {
        public Form_OperatorUpdate()
        {
            InitializeComponent();
        }
        public string OperatorName;
        private TicketSystemEntities tse = new TicketSystemEntities();
        private PublicMethod pm = new PublicMethod();
        private void Form_Update_Load(object sender, EventArgs e)
        {
            var ope = tse.Operators.Where(u => u.Name == OperatorName).FirstOrDefault();
            var role = tse.Roles.Where(u => true).Select(u => new { u.RoleID, u.RoleName }).ToList();
            this.com_Role.DataSource = role;
            this.com_Role.ValueMember = "RoleID";
            this.com_Role.DisplayMember = "RoleName";
            this.com_Role.SelectedValue = ope.RoleID;
            this.txt_IdCard.Text = ope.IDCard;
            this.txt_Name.Text = ope.Name;
            this.txt_Address.Text = ope.Address;
            this.txt_Phone.Text = ope.Phone;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (pm.IsTelephone(this.txt_Phone.Text))
            {
                if (pm.IsIDNumber(this.txt_IdCard.Text))
                {
                    var ope = tse.Operators.Where(u => u.Name == OperatorName).FirstOrDefault();
                    ope.RoleID = Convert.ToInt32(this.com_Role.SelectedValue);
                    ope.Name = this.txt_Name.Text;
                    ope.IDCard = this.txt_IdCard.Text;
                    ope.Phone = this.txt_Phone.Text;
                    ope.Address = this.txt_Address.Text;
                    ope.Updated_at = System.DateTime.Now;
                    try
                    {
                        if (tse.SaveChanges() >= 1)
                        {
                            MessageBox.Show("修改成功");
                        }
                        else
                        {
                            MessageBox.Show("你当前没有做任何修改");
                        }
                        this.Close();
                    }
                    catch
                    {
                        MessageBox.Show("更新失败了");
                    }
                  
                }
                else
                {
                    MessageBox.Show("身份证号输入有误");
                }
            }
            else
            {
                MessageBox.Show("手机号输入有误");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}
